package kr.hkit.summary_java.erp.serviceui;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.sql.Connection;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import kr.hkit.myframework.db.ConnectionFactory;
import kr.hkit.myframework.uicomp.InputPanel;
import kr.hkit.summary_java.erp.dao.DaoInterface;
import kr.hkit.summary_java.erp.dao.DepartmentDao;
import kr.hkit.summary_java.erp.dto.Department;

public class PanelDepartmentUI extends JPanel implements ActionListener {
	private InputPanel pDeptNo;
	private InputPanel pDeptName;
	private InputPanel pFloor;
	private JPanel panel;
	private JPanel panel_1;
	private JButton btn;

	private Connection con = ConnectionFactory.getConnection();
	private DaoInterface<Department> dao = new DepartmentDao(con);

	public void setBtn(String s) {
		this.btn.setText(s);
	}

	public void setDao(DaoInterface<Department> dao) {
		this.dao = dao;
	}

	/**
	 * Create the panel.
	 */
	public PanelDepartmentUI(String s) {
		initialize(s);
	}

	private void initialize(String s) {

		setLayout(new BorderLayout(0, 0));

		panel = new JPanel();
		add(panel, BorderLayout.CENTER);
		panel.setLayout(new GridLayout(0, 1, 0, 0));

		pDeptNo = new InputPanel("부서번호");
		panel.add(pDeptNo);
		pDeptNo.getTf().addFocusListener(new FocusAdapter() {

			@Override
			public void focusLost(FocusEvent e) {				//번호 입력 후 탭키 누를시
				super.focusLost(e);								
				Department d = null;
				try {
					d = dao.searchItem(new Department(Integer.parseInt(pDeptNo
							.getTf().getText().trim())));
					if (d != null) {							//해당 번호의 정보가 있으면 자동으로 빈칸 삽입
						pDeptNo.setTf(d.getDeptNo() + "");
						pDeptName.setTf(d.getDeptName());
						pFloor.setTf(d.getFloor() + "");
						if (btn.getText() == "검색") {
							panelEditable(false);
						}
					} else if (btn.getText() != "추가") {		//추가를 선택한 경우엔 제외
						JOptionPane.showMessageDialog(PanelDepartmentUI.this,
								"없는 정보");
						clsPanel();
					}
				} catch (NumberFormatException | SQLException e1) {
					e1.printStackTrace();
				}
			}
		});

		pDeptName = new InputPanel("부서명");
		panel.add(pDeptName);

		pFloor = new InputPanel("위치");
		panel.add(pFloor);

		panel_1 = new JPanel();
		add(panel_1, BorderLayout.SOUTH);

		btn = new JButton();
		setBtn(s);
		btn.addActionListener(this);
		panel_1.add(btn);

	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btn) {
			try {
				actionPerformedBtnNewButton(e);
			} catch (SQLException e1) {
				System.out.println("Fail!");
			}
		}
	}

	protected void actionPerformedBtnNewButton(ActionEvent e)
			throws SQLException {
		int dno;
		String deptName;
		int floor;
		Department d = null;
		try {
			switch (btn.getText()) {
				case "추가":
					dno = Integer.parseInt(pDeptNo.getTf().getText().trim());
					deptName = pDeptName.getTf().getText();
					floor = Integer.parseInt(pFloor.getTf().getText().trim());
					d = new Department(dno, deptName, floor);
					dao.insertItem(d);
					JOptionPane.showMessageDialog(this, "추가 성공");
					clsPanel();
					break;
				case "삭제":
					dno = Integer.parseInt(pDeptNo.getTf().getText().trim());
					d = new Department(dno);
					dao.deleteItemByNo(d);
					JOptionPane.showMessageDialog(this, "삭제 성공");
					clsPanel();
					break;
				case "수정":
					dno = Integer.parseInt(pDeptNo.getTf().getText().trim());
					deptName = pDeptName.getTf().getText();
					floor = Integer.parseInt(pFloor.getTf().getText().trim());
					d = new Department(dno, deptName, floor);
					dao.updateItemByNo(d);
					JOptionPane.showMessageDialog(this, "수정 성공");
					clsPanel();
					break;
				case "검색":
					dno = Integer.parseInt(pDeptNo.getTf().getText().trim());
					d = dao.searchItem(new Department(dno));
					if (d != null) {
						pDeptNo.setTf(d.getDeptNo() + "");
						pDeptName.setTf(d.getDeptName());
						pFloor.setTf(d.getFloor() + "");
					} else {
						JOptionPane.showMessageDialog(this, "없는 정보");
						pDeptNo.setTf("");
					}
					break;
			}
		} catch (NumberFormatException e1) {
			JOptionPane.showMessageDialog(this, "다시 입력하세요");
			clsPanel();
		}
	}

	private void panelEditable(boolean set) {
		if (set) {
			pDeptNo.getTf().setEditable(true);
			pDeptName.getTf().setEditable(true);
			pFloor.getTf().setEditable(true);
		} else {
			pDeptNo.getTf().setEditable(false);
			pDeptName.getTf().setEditable(false);
			pFloor.getTf().setEditable(false);
		}
	}

	private void clsPanel() {
		pDeptNo.setTf("");
		pDeptName.setTf("");
		pFloor.setTf("");
	}
}
